package com.kerrykidz.authority.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kerrykidz.authority.dao.mapper.AuSecUsrEntityMapper;
import com.kerrykidz.authority.dao.model.GblSecBranEntity;
import com.kerrykidz.authority.dao.model.SecObj2usrEntity;
import com.kerrykidz.authority.dao.model.SecUsrEntity;
import com.kerrykidz.authority.service.IAuUserService;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.GenerationUUID;

@Service
public class AuUserServiceImpl implements IAuUserService {
	
	@Autowired
	private AuSecUsrEntityMapper userMapper;
	

	@Override
	public SecUsrEntity findByUserEntity(SecUsrEntity user) throws RDPException {
		return userMapper.getByUser(user);
	}
	
	public List<SecUsrEntity> findLikeUser(SecUsrEntity entity)throws RDPException{
		return userMapper.getLikeUser(entity);
	}

	@Override
	public SecUsrEntity findByUserId(String pkUsrId) throws RDPException {
		return userMapper.getByUserId(pkUsrId);
	}

	@Override
	public List<GblSecBranEntity> findGblSecBranAll() throws RDPException {
		return userMapper.getGblSecBranAll();
	}

	@Override
	public void savetUser(SecUsrEntity entity) throws RDPException {
		entity.setLoginPwd(SecUsrEntity.DEFULAT_PASS);
		entity.setDelFlag(SecUsrEntity.DEL_FLAG_FALSE);
		entity.setPkUsrId(GenerationUUID.getGenerationUUID());
		
		if(entity.getDiscountEna().equals("0")){
			entity.setDiscountPwd("");
		}
		
		userMapper.insertUser(entity);
		
		this.saveObj(entity.getObjList(), entity.getPkUsrId());
		this.saveRole(entity.getRoleList(), entity.getPkUsrId());
		
	}

	@Override
	public void updateUser(SecUsrEntity entity) throws RDPException {
		entity.setDelFlag(SecUsrEntity.DEL_FLAG_FALSE);
		SecUsrEntity dbEntity = userMapper.getByUserId(entity.getPkUsrId());
		if(entity.getDiscountEna().equals("0")){
			entity.setDiscountPwd("");
		}
		dbEntity.setFkBranId(entity.getFkBranId());
		dbEntity.setUsrName(entity.getUsrName());
		dbEntity.setLoginId(entity.getLoginId());
		dbEntity.setEmail(entity.getEmail());
		dbEntity.setPhone(entity.getPhone());
		dbEntity.setJobLvl(entity.getJobLvl());
		dbEntity.setDiscountEna(entity.getDiscountEna());
		dbEntity.setDiscountPwd(entity.getDiscountPwd());
		dbEntity.setImagePath(entity.getImagePath());
		userMapper.updateUser(dbEntity);
		
		userMapper.deleteUserObj(entity.getPkUsrId());
		userMapper.deleteUserRole(entity.getPkUsrId());
		
		this.saveObj(entity.getObjList(), entity.getPkUsrId());
		this.saveRole(entity.getRoleList(), entity.getPkUsrId());
	}
	/**
	 * 保存权限
	 * @param objList
	 * @param userId
	 * @throws RDPException 
	 * @date: 2014年9月18日
	 */
	private void saveObj(List<SecObj2usrEntity> objList,String userId) throws RDPException{
		if(objList != null && objList.size() > 0){
			userMapper.insertUserObj(objList, userId);
		}
	}
	/**
	 * 保存角色
	 * @param objList
	 * @param userId
	 * @throws RDPException 
	 * @date: 2014年9月18日
	 */
	private void saveRole(List<String> roleList,String userId) throws RDPException{
		if(roleList != null && roleList.size() > 0){
			userMapper.insertUserRole(roleList, userId);
		}
	}

	@Override
	public void deleteUser(SecUsrEntity entity) throws RDPException {
		userMapper.deleteUser(entity);
	}

	@Override
	public List<SecObj2usrEntity> findByUserIdObjAll(String userId) throws RDPException {
		return userMapper.getByUserIdObjAll(userId);
	}

	@Override
	public List<String> findByUserIdRoleAll(String userId) throws RDPException {
		return userMapper.getByUserIdRoleAll(userId);
	}

	@Override
	public int updateResetPass(String userId,String updateUserId) throws RDPException {
		SecUsrEntity dbUser = userMapper.getByUserId(userId);
		if(userId != null ){
			dbUser.setLoginPwd(SecUsrEntity.DEFULAT_PASS);
			dbUser.setPwdFlag("1"); 
			dbUser.setModiBy(updateUserId);
			userMapper.updateUser(dbUser);
			return 1;
		}
		return 0;
	}

	@Override
	public void deleteUser(String userId,String updateUserId) throws RDPException {
		SecUsrEntity dbUser = userMapper.getByUserId(userId);
		if(userId != null ){
			dbUser.setDelFlag(SecUsrEntity.DEL_FLAG_TRUE);
			dbUser.setModiBy(updateUserId);
			userMapper.updateUser(dbUser);
		}
	}


}
